package com.hemerasolutions.v2trainer.model.equipment;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;
import com.hemerasolutions.v2trainer.model.library.Library;

@Entity
public class VersionableEquipment extends BaseModel {

	private static final long serialVersionUID = 1L;

    @OneToMany(mappedBy="versionableEquipment",cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Equipment> equipmentVersions;

    @OneToOne
    private Equipment currentEquipment;
    
    @ManyToOne
    private Library library;
    
    public VersionableEquipment() {
		super();
		this.equipmentVersions = new ArrayList<Equipment>();
	}

	public VersionableEquipment(VersionableEquipment other, Library library){
		this.library = library;
		
		for(Equipment equipment: other.equipmentVersions){
			
			Equipment aux = new Equipment(equipment,this);
			
			this.equipmentVersions.add(aux);

			if(equipment.equals(other.currentEquipment)){
				this.currentEquipment = aux;
			}
		}
	}

	public List<Equipment> getEquipmentVersions() {
		return equipmentVersions;
	}

	public void setEquipmentVersions(List<Equipment> equipmentVersions) {
		this.equipmentVersions = equipmentVersions;
	}

	public Equipment getCurrentEquipment() {
		return currentEquipment;
	}

	public void setCurrentEquipment(Equipment currentEquipment) {
		this.currentEquipment = currentEquipment;
	}

	public Library getLibrary() {
		return library;
	}

	public void setLibrary(Library library) {
		this.library = library;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((library == null) ? 0 : library.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		VersionableEquipment other = (VersionableEquipment) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (library == null) {
			if (other.library != null) {
				return false;
			}
		} else if (!library.equals(other.library)) {
			return false;
		}
		if (uuid == null) {
			if (other.uuid != null) {
				return false;
			}
		} else if (!uuid.equals(other.uuid)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "VersionableEquipment [id=" + id + ", uuid=" + uuid
				+ ", library=" + library + "]";
	}

	
}
